//
//  ECCollectinoViewStyle.swift
//  EasyCoding
//
//  Created by JY_NEW on 2020/7/6.
//

import UIKit

extension UCSetting where TargetType: UICollectionView {
    ///选择
    public static func selection(_ allows:Bool = true) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.allowsSelection = allows
        })
    }
    ///选择
    public static func selection(multiple allows:Bool) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.allowsMultipleSelection = allows
        })
    }
    ///注册Cell缓存类型
    public static func cell(_ cls: AnyClass, identifier: String = UCCellDefaultIdentifier) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.register(cls, forCellWithReuseIdentifier: identifier)
        })
    }
    ///注册Header缓存类型
    public static func header(_ cls: AnyClass, identifier: String = UCCellDefaultIdentifier) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.register(cls, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: identifier)
        })
    }
    ///注册Footer缓存类型
    public static func footer(_ cls: AnyClass, identifier: String = UCCellDefaultIdentifier) -> UCSetting<TargetType> {
        return .init(action: { (target) in
        target.register(cls, forSupplementaryViewOfKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: identifier)
        })
    }
    ///委托
    public static func delegate(_ delegate: UICollectionViewDelegate?) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.delegate = delegate
        })
    }
    ///数据源
    public static func source(_ dataSource: UICollectionViewDataSource?) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.dataSource = dataSource
        })
    }
    ///背景视图
    public static func bg(_ view: UIView) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.backgroundView = view
        })
    }
}
extension UCSetting where TargetType: UICollectionView {
    ///配置流布局
    public static func layout(flow layouts: UCSetting<UICollectionViewFlowLayout>...) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            if let layout = target.collectionViewLayout as? UICollectionViewFlowLayout {
                layout.set(animated: 0, settings: layouts)
            }
        })
    }
}
extension UCSetting where TargetType: UICollectionViewFlowLayout {
    /// 行间距
    public static func spacing(line spacing: CGFloat) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.minimumLineSpacing = spacing
        })
    }
    /// 单元格间距
    public static func spacing(item spacing: CGFloat) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.minimumInteritemSpacing = spacing
        })
    }
    /// 单元格大小
    public static func size(_ item: CGSize) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.itemSize = item
        })
    }
    /// 自动适应默认单元格大小
    public static func size(estimated item: CGSize) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.estimatedItemSize = item
        })
    }
    /// 滚动方向
    public static func direction(_ direction: UICollectionView.ScrollDirection) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.scrollDirection = direction
        })
    }
    /// Section头部高度
    public static func size(header size: CGSize) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.headerReferenceSize = size
        })
    }
    /// Section底部高度
    public static func size(footer size: CGSize) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.footerReferenceSize = size
        })
    }
    /// Section边距
    public static func edge(inset: UIEdgeInsets) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.sectionInset = inset
        })
    }
    /// Section边距相对位置
    @available(iOS 11.0, *)
    public static func edge(ref: UICollectionViewFlowLayout.SectionInsetReference) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.sectionInsetReference = ref
        })
    }
    /// 滚动时固定Section头部不动
    public static func pin(header enalbed: Bool) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.sectionHeadersPinToVisibleBounds = enalbed
        })
    }
    /// 滚动时固定Section底部不动
    public static func pin(footer enalbed: Bool) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.sectionFootersPinToVisibleBounds = enalbed
        })
    }
}
